package com.atguigu.mall.weball.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.cart.CartFeignClient;
import com.atguigu.gmall.product.entity.SkuInfo;
import feign.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestParam;

/**
 * TODO 由于weball 每次接收到一个请求，都会分配一个线程来处理
 */
@Controller
public class cartController {
    @Autowired
    private CartFeignClient cartFeignClient;
    @GetMapping("/addCart.html")
    public String addCart(@RequestParam("skuId") Long skuId,
                          @RequestParam("skuNum") Integer  skuNum
        , Model model){
        //所以逻辑是否在同一个线程
        Request.Options options = new Request.Options(5000,5000);
        Result<SkuInfo> result = cartFeignClient.addToCart(skuId, skuNum,options);
        //远程调用 订单服务
         model.addAttribute("skuInfo",result.getData());
         model.addAttribute("skuNum",skuNum);

           return   "cart/addCart";
    }

    /**
     * 购物车列表页
     * @return
     */
    @GetMapping("/cart.html")
     public  String  getCartList(){

        return "cart/index";
     }
    /**
     * 删除选中的商品
     * @return
     */
    @GetMapping("/cart/deleteChecked")
    public String deleteChecked(){

        cartFeignClient.deleteChecked();

        return "redirect:http://cart.gmall.com/cart.html";
    }

}
